package com.cn.yugan.Activity;


import android.os.AsyncTask;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

import com.cn.yugan.Engine.Adapter.HistoryListViewAdpater;
import com.cn.yugan.mobile.R;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Rover on 2016/7/22 .
 * Email 357319517@qq.com
 */
public class HistoryActivity extends AppCompatActivity {
    private String title;
    public static final int MENU_DELECT = 1;
    public static final int MENU_NORMAL = 0;
    //    static final int MENU_MANUAL_REFRESH = 0;
//    static final int MENU_DISABLE_SCROLL = 1;
//    static final int MENU_SET_MODE = 2;
//    static final int MENU_DEMO = 3;
    private int menustatus = 0; //菜单状态
    private PullToRefreshListView mPullRefreshListView;
    private HistoryListViewAdpater madapter;
    private List<Map<String, Object>> mlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("历史纪录");
        actionBar.setDisplayHomeAsUpEnabled(true);
       
        mlist = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map map = new HashMap<String, Object>();
            map.put("title", "haha");
            map.put("commit", "200");
            map.put("author", "haha");
            map.put("active", "原创");
            map.put("see", "100");
            mlist.add(map);
        }
        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.history_lv);
        madapter = new HistoryListViewAdpater(this, mlist, MENU_NORMAL);
        mPullRefreshListView.setAdapter(madapter);
        mPullRefreshListView.setMode(Mode.PULL_FROM_END);
        ILoadingLayout endLabels = mPullRefreshListView.getLoadingLayoutProxy(
                false, true);
        endLabels.setPullLabel("上拉我刷新(*^__^*) ");// 刚下拉时，显示的提示    
        endLabels.setRefreshingLabel("鱼干娘寻找中...");// 刷新时    
        endLabels.setReleaseLabel("放开我刷新((‵□′)) ");// 下来达到一定距离时，显示的提示
        
        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener());
    }
    


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_nomal_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        switch (menustatus) {
            case MENU_DELECT:
                menu.clear();
                inflater.inflate(R.menu.action_delect_menu, menu);
                break;
            case MENU_NORMAL:
                menu.clear();
                inflater.inflate(R.menu.action_nomal_menu, menu);
                break;
            default:
                menu.clear();
                inflater.inflate(R.menu.action_nomal_menu, menu);
                break;
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
//            case R.id.action1_delete:
//                menustatus = MENU_DELECT;
//                invalidateOptionsMenu();
//                madapter.setstatus(HistoryActivity.MENU_DELECT);
//                madapter.notifyDataSetChanged();
//                break;
            case android.R.id.home:
                this.finish();
                break;
//            case R.id.action2_delete:
//                //删除逻辑
//                //设置成原样
//                menustatus = MENU_NORMAL;
//                invalidateOptionsMenu();
//                madapter.setstatus(HistoryActivity.MENU_NORMAL);
//                madapter.notifyDataSetChanged();
//                madapter.notifyAll();
//                break;
//            case R.id.action2_cannel:
//                menustatus = MENU_NORMAL;
//                invalidateOptionsMenu();
//                madapter.setstatus(HistoryActivity.MENU_NORMAL);
//                madapter.notifyDataSetChanged();
//                break;
        }
        return true;
    }

    private class OnRefreshListener implements PullToRefreshBase.OnRefreshListener<ListView> {
        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                new GetDataTask().execute();
            }
        
    }

    //网络异步
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String[] result) {
            // mAdapter.notifyDataSetChanged();

            // Call onRefreshComplete when the list has been refreshed.
            mPullRefreshListView.onRefreshComplete();

            super.onPostExecute(result);
        }

    }
}